package com.dein.music.controller;


import com.dein.music.model.Music;
import com.dein.music.model.MusicAccumulation;
import com.dein.music.service.MusicAccumulationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("musicAccumulation/")
public class MusicAccumulationController {
    private MusicAccumulationService musicAccumulationService;

    @Autowired
    @Qualifier("musicAccumulationService")
    public void setMusicAccumulationService(MusicAccumulationService musicAccumulationService) {
        this.musicAccumulationService = musicAccumulationService;
    }

    @RequestMapping(value = "getAccumulationMusic", method = RequestMethod.POST)
    public
    @ResponseBody
    Object getAccumulationMusic(
            @RequestParam(value = "userId") Long userId,
            @RequestParam(value = "limit") Integer limit,
            @RequestParam(value = "start") Integer start) {
        List<Music> musicList = new ArrayList<Music>();

        musicList = musicAccumulationService.getAccumulationMusic(userId,limit,start);
        return musicList;
    }

    @RequestMapping(value = "addAccumulationMusic", method = RequestMethod.POST)
    public
    @ResponseBody
    Object addAccumulationMusic(
            @RequestParam(value = "userId") Long userId,
            @RequestParam(value = "musicId")Long musicId) {
        Map<String, Object> map = new HashMap<String, Object>();
        if(musicAccumulationService.accumulationMusicExist(userId, musicId)){
            map.put("result",true);
            return  map;
        }
        boolean result = musicAccumulationService.addAccumulationMusic(userId, musicId);
        map.put("result",result);
        return map;
    }

    @RequestMapping(value = "countAccumulationMusic",method = RequestMethod.POST)
    public @ResponseBody
    Object countAccumulationMusic(@RequestParam(value = "userId")Long userId){
        Integer count = musicAccumulationService.countAccumulationMusic(userId);
        return count;
    }
}
